﻿Imports System.Data.SqlClient

Public Class frmQLTaiKhoan

    Private Sub frmQLTaiKhoan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listTK As New DataTable()

        listTK = TaiKhoanNguoiDungBUS.GetAll()
        DataGridViewX1.DataSource = listTK

        Dim tentaikhoan As New List(Of LoaiTaiKhoanDTO)
        tentaikhoan = LoaiTaiKhoanBUS.GetAll()
        cbxTenLoaiTK.DataSource = tentaikhoan
        cbxTenLoaiTK.ValueMember = "iMaLoaiTaiKhoan"
        cbxTenLoaiTK.DisplayMember = "strTenLoaiTaiKhoan"
    End Sub

    Private Sub DataGridViewX1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewX1.SelectionChanged
        txtTenTK.Text = DataGridViewX1.CurrentRow.Cells("tentaikhoan").Value
        txtMatKhau.Text = DataGridViewX1.CurrentRow.Cells("matkhau").Value
        cbxTenLoaiTK.Text = DataGridViewX1.CurrentRow.Cells("loaitaikhoan").Value

    End Sub




    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If Not txtMatKhau.Text = "" Then
            If Not txtTenTK.Text = "" Then

                Dim sqlparameters(2) As SqlParameter
                Dim QLTaiKhoan As TaiKhoanNguoiDungDTO = New TaiKhoanNguoiDungDTO()

                QLTaiKhoan.strTenTaiKhoan = txtTenTK.Text

                QLTaiKhoan.strMatKhau = txtMatKhau.Text
                QLTaiKhoan.iMaLoaiTaiKhoan = cbxTenLoaiTK.SelectedValue


                Dim para0 As New SqlParameter("tentaikhoan", SqlDbType.VarChar)
                para0.Direction = ParameterDirection.Input
                para0.Value = QLTaiKhoan.strTenTaiKhoan
                sqlparameters(0) = para0


                Dim para1 As New SqlParameter("matkhau", SqlDbType.VarChar)
                para1.Direction = ParameterDirection.Input
                para1.Value = QLTaiKhoan.strMatKhau
                sqlparameters(1) = para1
                Dim para2 As New SqlParameter("maloaitaikhoan", SqlDbType.Int)
                para2.Direction = ParameterDirection.Input
                para2.Value = QLTaiKhoan.iMaLoaiTaiKhoan
                sqlparameters(2) = para2
                TaiKhoanNguoiDungBUS.Insert(sqlparameters)
                MessageBox.Show("Thêm Thành Công")

                Dim listTK As New DataTable()

                listTK = TaiKhoanNguoiDungBUS.GetAll()
                DataGridViewX1.DataSource = listTK
            Else
                MsgBox("Bạn chưa nhập vào tên tài khoản ", MsgBoxStyle.Critical, "Thông báo!")

                txtTenTK.Text = Focus()

            End If
        Else
            MsgBox("Bạn chưa nhập vào mật khẩu ", MsgBoxStyle.Critical, "Thông báo!")

            txtMatKhau.Text = Focus()
        End If

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If Not txtMatKhau.Text = "" Then
            If Not txtTenTK.Text = "" Then

                Dim sqlparameters(3) As SqlParameter
                Dim QLTaiKhoan As TaiKhoanNguoiDungDTO = New TaiKhoanNguoiDungDTO()
                QLTaiKhoan.iMaTaiKhoan = DataGridViewX1.CurrentRow.Cells("MaTaiKhoan").Value
                QLTaiKhoan.strTenTaiKhoan = txtTenTK.Text
                QLTaiKhoan.strMatKhau = txtMatKhau.Text
                QLTaiKhoan.iMaLoaiTaiKhoan = cbxTenLoaiTK.SelectedValue


                Dim para0 As New SqlParameter("mataikhoan", SqlDbType.Int)
                para0.Direction = ParameterDirection.Input
                para0.Value = QLTaiKhoan.iMaTaiKhoan
                sqlparameters(0) = para0

                Dim para1 As New SqlParameter("tentaikhoan", SqlDbType.VarChar)
                para1.Direction = ParameterDirection.Input
                para1.Value = QLTaiKhoan.strTenTaiKhoan
                sqlparameters(1) = para1


                Dim para2 As New SqlParameter("matkhau", SqlDbType.VarChar)
                para2.Direction = ParameterDirection.Input
                para2.Value = QLTaiKhoan.strMatKhau
                sqlparameters(2) = para2

                Dim para3 As New SqlParameter("maloaitaikhoan", SqlDbType.Int)
                para3.Direction = ParameterDirection.Input
                para3.Value = QLTaiKhoan.iMaLoaiTaiKhoan
                sqlparameters(3) = para3


                TaiKhoanNguoiDungBUS.Update(sqlparameters)
                MessageBox.Show("Sửa Thành Công")

                Dim listTK As New DataTable()

                listTK = TaiKhoanNguoiDungBUS.GetAll()
                DataGridViewX1.DataSource = listTK
            Else
                MsgBox("Bạn chưa nhập vào tên tài khoản ", MsgBoxStyle.Critical, "Thông báo!")

                txtTenTK.Text = Focus()

            End If
        Else
            MsgBox("Bạn chưa nhập vào mật khẩu ", MsgBoxStyle.Critical, "Thông báo!")

            txtMatKhau.Text = Focus()
        End If
    End Sub

  

    Private Sub btnXoa_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim taikhoan As TaiKhoanNguoiDungDTO = New TaiKhoanNguoiDungDTO()
        taikhoan.iMaTaiKhoan = DataGridViewX1.CurrentRow.Cells("MaTaiKhoan").Value


        Dim para1 As New SqlParameter("MaTaiKhoan", SqlDbType.VarChar)
        para1.Direction = ParameterDirection.Input
        para1.Value = taikhoan.iMaTaiKhoan


        TaiKhoanNguoiDungBUS.Delete(para1)
        Dim listTK As New DataTable()

        listTK = TaiKhoanNguoiDungBUS.GetAll()
        DataGridViewX1.DataSource = listTK
    End Sub
End Class